package com.irdstudio.efp.nls.service.impl.wsd.credit;

import com.alibaba.fastjson.JSON;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.service.bo.req.wsd.finalnotice.FinalApproveNoticeReqBean;
import com.irdstudio.efp.esb.service.facade.EsbInvokeWrapper;
import com.irdstudio.efp.esb.service.facade.wsd.finalnotice.FinalApproveNoticeService;
import com.irdstudio.efp.nls.service.facade.NlsPublicService;
import com.irdstudio.efp.nls.service.facade.NlsQueueSoltComnService;
import com.irdstudio.efp.nls.service.facade.WsApproveackConfirmService;
import com.irdstudio.efp.nls.service.impl.sx.CreditSoltServiceImpl;
import com.irdstudio.efp.nls.service.vo.WsApproveackConfirmVO;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessBizVO;
import com.irdstudio.efp.nls.service.vo.sx.NlsCreditInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


@Service("wsdCreditAccessService")
public class WsdCreditAccessServiceImpl extends CreditSoltServiceImpl implements FrameworkService {
    @Override
    protected void doExecute(NlsProcessBizVO nlsProcessBizVo, NlsCreditInfoVO nlsCreditInfoVO) throws Exception {

        String applySeq = nlsCreditInfoVO.getLmtApplySeq();
        logger.info("======>授信审批服务开始,流水号" + applySeq+"<======");

        /*###################################################################################################*/
        /********************************* 授信审批档插槽主流程 **********************************************/
        /*###################################################################################################*/
        try {
            // 获取终审审批信息
            WsApproveackConfirmVO wsApproveackConfirm = nlsPublicService.getWsApproveackConfirm(nlsCreditInfoVO.getLmtApplySeq());
            // 获取授信复审审批数据 从redis中获取
            if ("Y".equals(wsApproveackConfirm.getIsAgree())){
                return;
            }else {
                updateApproveackConfirm(nlsCreditInfoVO, wsApproveackConfirm);
                throw new BizException("执行授信审批规则失败!!!");
            }
        }catch(Exception e){
            logger.error("======>【授信审批服务】执行失败原因为<======", e.getMessage());
            nlsProcessBizVo.setExceptionFlag(Boolean.TRUE);
            nlsProcessBizVo.setRefuseMsg(e.getMessage());
            nlsProcessBizVo.setExceptionMsg(e);
        }finally {
            logger.info("======>授信审批服务结束,流水号<======" + applySeq);
        }
    }

    private void updateApproveackConfirm(NlsCreditInfoVO nlsCreditInfoVO, WsApproveackConfirmVO wsApproveackConfirm) throws ESBException, InterruptedException {
        FinalApproveNoticeReqBean finalApproveNoticeReqBean = new FinalApproveNoticeReqBean();
        finalApproveNoticeReqBean.setRqsIdmptntID(wsApproveackConfirm.getRequestId());
        finalApproveNoticeReqBean.setAplNo(wsApproveackConfirm.getApplyNo());
        finalApproveNoticeReqBean.setAdptCertNm(nlsCreditInfoVO.getCusName());
        finalApproveNoticeReqBean.setAdptCertNo(nlsCreditInfoVO.getCertCode());
        finalApproveNoticeReqBean.setCnsntFlg(wsApproveackConfirm.getIsAgree());
        finalApproveNoticeReqBean.setRjctnCd(wsApproveackConfirm.getRefuseCode());
        finalApproveNoticeReqBean.setAprvlRsltRsn(wsApproveackConfirm.getAckMsg());
        finalApproveNoticeReqBean.setExnFld("{}");
        EsbInvokeWrapper esbInvokeWrapper = new EsbInvokeWrapper(finalApproveNoticeService, finalApproveNoticeReqBean,nlsQueueSoltComnService);
        esbInvokeWrapper.invoke3Threes();
        // 更新终审应答信息表
        wsApproveackConfirm.setResultInfo(JSON.toJSONString(finalApproveNoticeReqBean));
        wsApproveackConfirmService.updateByPk(wsApproveackConfirm);
    }


    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(WsdCreditAccessServiceImpl.class);

    /**
     * 产品信息服务
     */
    @Autowired
    @Qualifier("nlsPublicService")
    private NlsPublicService nlsPublicService;

    /**
     * 终审应答服务
     */
    @Autowired
    @Qualifier("finalApproveNoticeService")
    private FinalApproveNoticeService finalApproveNoticeService;

    // 告警服务
    @Autowired
    @Qualifier("nlsQueueSoltComnService")
    private NlsQueueSoltComnService nlsQueueSoltComnService;

    // 应答信息
    @Autowired
    @Qualifier("wsApproveackConfirmService")
    private WsApproveackConfirmService wsApproveackConfirmService;
}
